package com.huawei.crm.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.huawei.crm.mapper.BaseRepository;
import com.huawei.crm.service.BaseService;

import java.io.Serializable;


/**
 * 通用Service接口默认实现
 *
 * @author luohua
 * @version 2021年11月24日10:55:36
 *
 * @param <R>
 * @param <T>
 * @param <ID>
 */
public abstract class AbstractService<R extends BaseRepository<T, ID>, T, ID extends Serializable>
		extends ServiceImpl<R, T> implements BaseService<T, ID> {

	@Override
	@SuppressWarnings("unchecked")
	protected Class<T> currentMapperClass() {
        return (Class<T>) this.getResolvableType().as(AbstractService.class).getGeneric(0).getType();
    }

	@Override
	@SuppressWarnings("unchecked")
    protected Class<T> currentModelClass() {
        return (Class<T>) this.getResolvableType().as(AbstractService.class).getGeneric(1).getType();
    }

	@Override
	public boolean save(T entity) {
		return SqlHelper.retBool(baseMapper.insertSelective(entity));
	}

	@Override
	public int update(T entity) {
		return baseMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public T findById(ID id) {
		return baseMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean deleteById(ID id) {
		return SqlHelper.retBool(baseMapper.deleteByPrimaryKey(id));
	}

}
